package com.xsp.service.Impl;

import com.xsp.dao.BankDao;
import com.xsp.entity.Bank;
import com.xsp.exception.BankException;
import com.xsp.service.BankService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * @author xsp
 * @date 2024/1/22
 * @desc
 */
//@Service
public class BankServiceImpl implements BankService {
    @Resource
    private BankDao bankDao;

    //    @Transactional(propagation = Propagation.NEVER)
    // 设置aop捕获的异常的类型
    @Transactional(rollbackFor = {Exception.class})
    @Override
    public boolean transferMoney(Bank fromAccount, Bank toBank, Integer money) throws Exception {
        // 扣款账户减去指定金额
        fromAccount.setMoney(fromAccount.getMoney() - money);
        // 扣款
        int result = bankDao.update(fromAccount);
        if (result > 0) {
            // 模拟异常
            int a = 10 / 0;
            // 收款
            toBank.setMoney(toBank.getMoney() + money);
            result = bankDao.update(toBank);
            if (result > 0) {
                return true;
            } else {
                throw new BankException("收款人信息不存在，转账撤销！");
            }
        } else {
            throw new BankException("转账人信息不存在，转账撤销！");
        }
    }

    @Override
    @Transactional
    public boolean remove(Integer id) {
        int delete = bankDao.delete(id);
        // 模拟异常
        int a = 10 / 0;
        return delete > 0;
    }
}
